package com.example.controller;

import com.example.pojo.ConditionsQuery;
import com.example.pojo.Patient;
import com.example.pojo.PolicyVo;
import com.example.pojo.Result;
import com.example.service.PolicyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/policy")
public class PolicyController {

    @Autowired
    private PolicyService policyService;

    @RequestMapping("/getList")
    public Result getList(@RequestBody ConditionsQuery conditionsQuery){


        PageHelper.startPage(conditionsQuery.getPageNum(),conditionsQuery.getPageSize());

        //System.err.println(doctorQuery);
        List<PolicyVo> list=policyService.getList(conditionsQuery);
        // System.err.println(list);

        PageInfo<PolicyVo> pageInfo=new PageInfo<>(list);

        Map<String,Object> resultMap=new HashMap<>();

        //当前页数据
        resultMap.put("list",pageInfo.getList());

        //数据总数
        resultMap.put("total",pageInfo.getTotal());

        return new Result(200,"数据加载成功",resultMap);
    }

    @RequestMapping("/add")
    public Result addPolicy(@RequestBody Map<String,String> policyVo){
        return policyService.addPolicy(policyVo);
    }

    /**
     * 只能修改标题和政策
     * 需要id ,title,message
     * @param newPolicy
     * @return
     */
    @RequestMapping("/edit")
    public Result editPolicy(@RequestBody Map<String,String> newPolicy){
        return policyService.editPolicy(newPolicy);
    }

    @RequestMapping("/delPolicyById")
    public Result delPolicyById(@RequestBody Map<String,Integer> map){
        int id =map.get("id");
        return policyService.delPolicyById(id);
    }
    @RequestMapping("/delBatch")
    public Result delBatch(@RequestBody Map<String,String> map){

        String ids = map.get("ids");

        return policyService.delBatch(ids);
    }
}
